package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.GetHttpMessage;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Scattergram;
import com.itheima.service.ScattergramService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.UUID;


/**
 * @Author yangsen
 * @desc 公司分布图控制层
 * @Date 2020/10/28 18:51
 */
@RestController
@RequestMapping("/scattergram")
public class ScattergramController {
	/*//使用JedisPool操作Redis服务
	@Autowired
	private JedisPool jedisPool;*/

	private GetHttpMessage getHttpMessage;


	//远程注入
	@Reference
	private ScattergramService scattergramService;


	/**
	 * 查询所有的公司地址
	 *
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {


		//查询所有的地址
		PageResult pageResult = scattergramService.findPage(queryPageBean);

		return pageResult;


	}


	/**
	 * 通过id查询详细改地址的详细信息
	 *
	 * @param id 查询的id   前端需要传递的数据
	 * @return
	 */
	@RequestMapping("/findById")
	public Result findById(Integer id) {

		try {
			//通过id查询数据库的详细的数据
			Scattergram scattergram = scattergramService.findById(id);

			return new Result(true, MessageConstant.QUERY_SCATTERAM_SUCCESS,scattergram);

		} catch (Exception e) {
			return new Result(false, MessageConstant.QUERY_SCATTERAM_FALL);
		}


	}


	/**
	 * 添加公司地址
	 *
	 * @param scattergram 公司地址的详细信息
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody Scattergram scattergram) {
		try {
			//调用控制层进行添加
			scattergramService.add(scattergram);

			return new Result(true, MessageConstant.ADD_SCATTERAM_SUCCESS);

		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.ADD_SCATTERAM_FALL);
		}
	}

	//文件上传
	@RequestMapping("/upload")
	public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
		System.out.println(imgFile);
		String originalFilename = imgFile.getOriginalFilename();//原始文件名
		int index = originalFilename.lastIndexOf(".");
		String extention = originalFilename.substring(index - 1);//.jpg
		String fileName = UUID.randomUUID().toString() + extention;//dhgahdhajdajd.jpg
		try {
			//将文件上传到七牛云
			QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
			/*jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);*/
		} catch (IOException e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
		}
		//返回给页面fileName进行显示
		return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
	}


	/**
	 * 修改地址的信息
	 *
	 * @param scattergram 修改后的值
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody Scattergram scattergram) {

		try {
			//调用控制层进行添加
			scattergramService.update(scattergram);

			return new Result(true, MessageConstant.UPDATE_SCATTERAM_SUCCESS);

		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.UPDATE_SCATTERAM_FALL);
		}

	}


	/**
	 * 修改地址的信息
	 *
	 * @param id 修改后的值
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Integer id) {
		try {
			//调用控制层进行删除
			scattergramService.delete(id);

			return new Result(true, MessageConstant.DELETE_SCATTERAM_SUCCESS);

		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.DELETE_SCATTERAM_FALL);
		}
	}
	//根据地址动态获取百度地图经纬度
	@RequestMapping("/getHttpMessage")
	public Result getHttpMessage(@RequestBody String address) {
		JSONObject get = null;
		try {
			String[] split = address.split("=");
			String s = split[0];
			String url = "http://api.map.baidu.com/geocoding/v3/?address=" + s + "&output=json&ak=Tr0lCMfb3FYT63Y4Doz6PFoV6pcN02r1";
			get = GetHttpMessage.httpRequest(url, "GET");
			return new Result(true, "响应数据请求成功", get);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "响应数据请求成功");
		}
	}
}
